package com.dbq.fast.util;

import android.annotation.TargetApi;
import android.app.AppOpsManager;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Binder;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;

import java.lang.reflect.Method;

import static android.content.Context.WINDOW_SERVICE;

public class FitBottomNavUtil {
    // For more information, see https://code.google.com/p/android/issues/detail?id=5497
    // To use this class, simply invoke assistActivity() on an Activity that already has its content view set.
//    private static boolean isKeyBordVisiable;

    Context context;

    private View mChildOfContent;
    private int usableHeightPrevious;
    private ViewGroup.LayoutParams frameLayoutParams;

    public FitBottomNavUtil() {

    }

    public void assistActivity(Context context, View content){
        this.context = context;
        if (isXiaomiRom()) {
            return;
        }
        mChildOfContent = content;
        mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                possiblyResizeChildOfContent();
            }
        });
        frameLayoutParams = mChildOfContent.getLayoutParams();
    }

    private void possiblyResizeChildOfContent() {
        int usableHeightNow = computeUsableHeight();
        if (usableHeightNow != usableHeightPrevious) {
            //如果两次高度不一致
//            int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
//            int heightDifference = usableHeightSansKeyboard - usableHeightNow;
//            if (heightDifference > (usableHeightSansKeyboard / 4)) {
//                // keyboard probably just became visible
//                isKeyBordVisiable=true;
//            } else {
//                // keyboard probably just became hidden
//                isKeyBordVisiable=false;
//            }
            //将计算的可视高度设置成视图的高度
            frameLayoutParams.height = usableHeightNow;
            mChildOfContent.requestLayout();//请求重新布局
            usableHeightPrevious = usableHeightNow;
        }
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    private int computeUsableHeight() {
        //计算视图可视高度
        Rect r = new Rect();
        mChildOfContent.getWindowVisibleDisplayFrame(r);
        int height = r.bottom;
//        Log.e("vpn_log", "bottom:"+r.bottom+" top:"+r.top);
        if(!hasSoftKeys(context)){
            if(needResize()){
                height = r.bottom - r.top;
            }
        }
        return height;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    public boolean hasSoftKeys(Context context){
        WindowManager windowManager = (WindowManager) context.getSystemService(WINDOW_SERVICE);
        Display d = windowManager.getDefaultDisplay();

        Point size = new Point();
        windowManager.getDefaultDisplay().getRealSize(size);
        int realWidth = size.x;
        int realHeight = size.y;

        windowManager.getDefaultDisplay().getSize(size);
        int displayWidth = size.x;
        int displayHeight = size.y;


//        DisplayMetrics realDisplayMetrics = new DisplayMetrics();
//        d.getRealMetrics(realDisplayMetrics);
//
//        int realHeight = realDisplayMetrics.heightPixels;
//        int realWidth = realDisplayMetrics.widthPixels;
//
//        DisplayMetrics displayMetrics = new DisplayMetrics();
//        d.getMetrics(displayMetrics);
//
//        int displayHeight = displayMetrics.heightPixels;
//        int displayWidth = displayMetrics.widthPixels;

//        Log.e("vpn_log", "realWidth:"+((realWidth - displayWidth) > 0) );
//        Log.e("vpn_log", "realHeight:"+((realHeight - displayHeight) > 0) );

        return (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0;
    }

    private boolean needResize(){
Log.e("yc_", "~~~ model:"+Build.MODEL);

        if("vivo".equals(Build.BRAND)){
            return false;
        }else if("HUAWEI".equals(Build.BRAND) && isPad(context)){
            return false;
        }else if("nubia".equals(Build.BRAND)){
            return false;
        }else if("OPPO A37m".equals(Build.MODEL)){
            return false;
        }else if("HONOR".equals(Build.BRAND)){
            return false;
        }else if("360".equals(Build.BRAND)){
            return false;
        }else if(Build.BRAND.contains("Xiaomi")){
            return false;
        }else if("SM-G9250".equals(Build.MODEL)){
            return false;
        }

        return true;
    }

    public boolean isPad(Context context) {
        return (context.getResources().getConfiguration().screenLayout
                & Configuration.SCREENLAYOUT_SIZE_MASK)
                >= Configuration.SCREENLAYOUT_SIZE_LARGE;
    }

    private boolean isXiaomiRom() {
        return TextUtils.equals(Build.BRAND, "Xiaomi") && !getSystemModel().contains("MIX");
    }
    /**
     * 获取手机型号
     *
     * @return  手机型号
     */
    public static String getSystemModel() {
        return android.os.Build.MODEL;
    }

    /***
     * 检测悬浮窗是否开启
     * @param context
     * @return
     */
    public static boolean getAppOps(Context context) {
        try {
            Object object = context.getSystemService("appops");
            if (object == null) {
                return false;
            }
            Class localClass = object.getClass();
            Class[] arrayOfClass = new Class[3];
            arrayOfClass[0] = Integer.TYPE;
            arrayOfClass[1] = Integer.TYPE;
            arrayOfClass[2] = String.class;
            Method method = localClass.getMethod("checkOp", arrayOfClass);
            if (method == null) {
                return false;
            }
            Object[] arrayOfObject1 = new Object[3];
            arrayOfObject1[0] = Integer.valueOf(24);
            arrayOfObject1[1] = Integer.valueOf(Binder.getCallingUid());
            arrayOfObject1[2] = context.getPackageName();
            int m = ((Integer) method.invoke(object, arrayOfObject1)).intValue();
            return m == AppOpsManager.MODE_ALLOWED;
        } catch (Exception ex) {

        }
        return false;
    }

}
